<?php

namespace app\common\model\Product;

use think\Model;

class Product extends Model
{
    // 指定的数据表
    protected $table = 'pre_product';

    protected $append = [
        'thumbs_cdn', // 多图
        'cover_cdn', // 单图
        'vip_price'
    ];

    public function getCoverCdnAttr($value,$data)
    {
        $cdn = config('cdn');

        $thumbs = empty($data['thumbs']) ? [] : trim($data['thumbs']);

        if(empty($thumbs))
        {
            return $cdn .'/static/home/res/images/avatar/default.png';
        }

        //字符串替换
        $thumbs = str_replace("uploads/", $cdn."/uploads/", $thumbs);

        //在转换为数组
        $list = explode(',', $thumbs);

        if(count($list) > 0)
        {
            return $list[0];
        }

    }

    public function getThumbsCdnAttr($value,$data)
    {
        $cdn = config('cdn');

        $thumbs = empty($data['thumbs']) ? [] : trim($data['thumbs']);

        if(empty($thumbs))
        {
            return [$cdn .'/static/home/res/images/avatar/default.png'];
        }

        //字符串替换
        $thumbs = str_replace("uploads/", $cdn."/uploads/", $thumbs);

        //在转换为数组
        $list = explode(',', $thumbs);

        return $list;
    }

    // 会员价格
    public function getVipPriceAttr($value,$data)
    {
        $price = model('Product.Vip')->where(['proid' => $data['id']])->value('price');

        if(!empty($price))
        {
            return $price;
        }else{
            return 0.00;
        }
    }

    public function type()
    {
        return $this->belongsTo('app\common\model\Product\Type','typeid','id',[],'LEFT')->setEagerlyType(0);
    }

    
}
